package com.sso.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.sso.entity.DicSystem;
import com.sso.entity.DicSystemUser;
import com.sso.managedBean.ListSystemBean;
import com.sso.service.IDicSystemService;
import com.sso.service.IDicSystemUserService;
@ManagedBean(name = "sso_listUsersBean", scope = ManagedBeanScope.REQUEST)
public class ListUsersBean {
	private final static Log logger = LogFactory
	.getLog(ListSystemBean.class);
	private IDicSystemUserService dicSystemUserService = SpringBeanHolder.
			getBean("dicSystemUserService");
	@Inject
	private FacesContext context;
	
	@Accessible
	@SaveState
	private DicSystemUser user;
	
	@Bind(id = "userNameForSearch", attribute = "value")
	@SaveState
	private String userName_value;
	
	@Bind(id = "listUsersGrid", attribute = "binding")
	private UIDataGrid usersGrid_binding;
	
	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			userName_value = null;
		}
	}
	@Action(id = "search")
	public void search() {
		usersGrid_binding.reload();
	}
	
	@Action(id = "reset")
	public void reset() {
		userName_value = null;
		usersGrid_binding.reload();
	}
	
	@DataModel(id = "listUsersGrid")
	private List<DicSystemUser> getUsersGrid() {
		// 搜索条件写入搜索
		List<DicSystemUser> chList = new ArrayList<DicSystemUser>();
		if (userName_value != null && !"".equals(userName_value)) {
			chList = dicSystemUserService.getByUserName(userName_value);
		} else {
			chList = dicSystemUserService.getAllSystemUser();
		}
		return chList;
	}
	
	@Action(id = "deleteUser")
	public void deleteUser() {
		Object[] obj = usersGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		DicSystemUser user = (DicSystemUser) obj[0];
		//逻辑删除
		dicSystemUserService.deleteDicSystemUser(user);
		usersGrid_binding.reload();
	}
	
	@Action(id = "eidtSystem")
	public void eidtSystem() {
		Object[] obj = usersGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		DicSystemUser user = (DicSystemUser) obj[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/sso/editUser.faces?id=" + user.getId();
		String script = "addCenterLayoutTab('eidtUserTab','编辑单点登录系统用户','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
	}
}
